﻿// ===============================================================================
//  Copyright © ymind.net. All rights reserved .
//  官方网站：http://ymind.net/
//  版权所有：彦铭工作室 老陈出品
// ===============================================================================

using System;
using System.Net;

namespace Lcsoft.UrlRewriter.Actions
{
	/// <summary>
	/// 提供设置 HTTP 状态码功能的地址重写动作。
	/// </summary>
	public class SetStatusAction : IRewriteAction
	{
		/// <summary>
		/// 初始化 <see cref="SetStatusAction"/> 类的新实例。
		/// </summary>
		/// <param name="statusCode">HTTP 状态码。</param>
		public SetStatusAction(HttpStatusCode statusCode) { this.StatusCode = statusCode; }

		/// <summary>
		/// 获取 HTTP 状态码。
		/// </summary>
		public HttpStatusCode StatusCode { get; private set; }

		#region IRewriteAction Members

		/// <summary>
		/// 执行动作。
		/// </summary>
		/// <param name="context"><see cref="RewriteContext"/> 对象的实例。</param>
		/// <returns>返回 <see cref="RewriteProcessing"/> 枚举值之一。</returns>
		public virtual RewriteProcessing Execute(RewriteContext context)
		{
			if (context == null) throw new ArgumentNullException("context");

			context.StatusCode = this.StatusCode;

			return (int)this.StatusCode >= 300 ? RewriteProcessing.StopProcessing : RewriteProcessing.ContinueProcessing;
		}

		#endregion
	}
}
